package com.tenghui.assisting.controller;

import java.math.BigDecimal;
import java.util.List;

import com.tenghui.assisting.vo.ReplenishVo;
import com.tenghui.assisting.vo.ShelfOperationVo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tenghui.common.annotation.Log;
import com.tenghui.common.core.controller.BaseController;
import com.tenghui.common.core.domain.AjaxResult;
import com.tenghui.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.tenghui.assisting.domain.Products;
import com.tenghui.assisting.service.IProductsService;
import com.tenghui.common.utils.poi.ExcelUtil;
import com.tenghui.common.core.page.TableDataInfo;

/**
 * 土特产产品Controller
 *
 * @author tenghui
 * @date 2025-10-27
 */
@RestController
@RequestMapping("/assisting/products")
public class ProductsController extends BaseController
{
    @Autowired
    private IProductsService productsService;

    /**
     * 查询土特产产品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Products products)
    {
        startPage();
        List<Products> list = productsService.selectProductsList(products);
        return getDataTable(list);
    }

    /**
     * 导出土特产产品列表
     */
    @Log(title = "土特产产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Products products)
    {
        List<Products> list = productsService.selectProductsList(products);
        ExcelUtil<Products> util = new ExcelUtil<Products>(Products.class);
        util.exportExcel(response, list, "土特产产品数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Products> util = new ExcelUtil<Products>(Products.class);
        util.importTemplateExcel(response, "土特产产品数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "土特产产品", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Products> util = new ExcelUtil<Products>(Products.class);
        InputStream inputStream = file.getInputStream();
        List<Products> list = util.importExcel(inputStream );
        inputStream.close();
        int count = productsService.batchInsertProducts(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取土特产产品详细信息
     */
    @GetMapping(value = "/{productsId}")
    public AjaxResult getInfo(@PathVariable("productsId") String productsId)
    {
        return success(productsService.selectProductsByProductsId(productsId));
    }

    /**
     * 新增土特产产品
     */
    @Log(title = "土特产产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Products products)
    {
        return toAjax(productsService.insertProducts(products));
    }

    /**
     * 修改土特产产品
     */
    @Log(title = "土特产产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Products products)
    {
        return toAjax(productsService.updateProducts(products));
    }

    /**
     * 删除土特产产品
     */
    @Log(title = "土特产产品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productsIds}")
    public AjaxResult remove(@PathVariable String[] productsIds)
    {
        return toAjax(productsService.deleteProductsByProductsIds(productsIds));
    }

    /**
     * 查询土特产产品列表（无权限）
     */
    @GetMapping("/selectAllList")
    public TableDataInfo selectAllList(Products products)
    {
        startPage();
        List<Products> list = productsService.selectAllList(products);
        return getDataTable(list);
    }
    /**
     * 补货商品
     */
    @Log(title = "补货", businessType = BusinessType.UPDATE)
    @PutMapping("/replenish")
    public AjaxResult replenish(@RequestBody ReplenishVo replenishVo) {
        // 补货数量必须大于0
        if(replenishVo.getCount().compareTo(BigDecimal.ZERO) <=0) return error("补货数量必须大于0");

        // 产品ID和数量
        String productsId = replenishVo.getProductsId();
        BigDecimal count = replenishVo.getCount();
        // 补货前的数量：根据产品ID拿到补货前的数量
        Products existingProduct = productsService.selectProductsByProductsId(productsId);
        if(existingProduct == null) return error("产品不存在，产品ID：" + productsId);

        Long oldInventory = existingProduct.getInventory();
        // 将Long类型的库存转换为BigDecimal，然后相加
        BigDecimal oldInventoryDecimal = BigDecimal.valueOf(oldInventory);
        BigDecimal newInventoryDecimal = oldInventoryDecimal.add(count);

        // 将BigDecimal转换回Long
        Long newInventory = newInventoryDecimal.longValue();
        // 更新库存
        existingProduct.setInventory(newInventory);
        // 保存到数据库
        int updateResult = productsService.updateProducts(existingProduct);
        return toAjax(updateResult);
    }

    /**
     * 上下架商品
     */
    @Log(title = "下架商品", businessType = BusinessType.UPDATE)
    @PutMapping("shelfOperation")
    public AjaxResult shelfOperation(@RequestBody ShelfOperationVo shelfOperationVo) {
        System.out.println(shelfOperationVo);
        int updateResult = productsService.shelfOperation(shelfOperationVo);
        return toAjax(updateResult);
    }


}
